<template>
	<div class="mainsu">
		<div class="main_spac1">
			<div class="main_white padding10 paddingBottom20">
				<div class="mainor_form">
					<div class="mainor_te">
						<el-button type="primary" @click="exportform">导出</el-button>
					</div>
					<el-form
						:inline="true"
						:model="form"
						class="demo-form-inline"
						ref="userListFrom"
						label-width="70px"
					>
						<el-form-item label="" prop="date_type" class="wid110">
							<el-select v-model="form.date_type" placeholder="请选择">
								<el-option label="配送日期" value="1"> </el-option>
								<el-option label="下单日期" value="2"> </el-option>
							</el-select>
						</el-form-item>
						<el-form-item label="" prop="establishTime">
							<el-date-picker
								v-model="establishTime"
								type="daterange"
								class="time220"
								:editable="false"
								unlink-panels
								range-separator="至"
								start-placeholder="开始日期"
								end-placeholder="结束日期"
								value-format="yyyy-MM-dd"
								:clearable="false"
								:picker-options="pickerOptions"
								@change="dateChange"
							/>
						</el-form-item>
						<el-form-item label="支付方式" prop="is_piroment">
							<el-select
								v-model.trim="form.is_piroment"
								placeholder="支付方式"
								class="wid220"
							>
								<el-option label="所有" value=""></el-option>
								<el-option
									v-for="(is_piroment, index) in is_piroment"
									:key="index"
									:label="is_piroment"
									:value="parseInt(index)"
								></el-option>
							</el-select>
						</el-form-item>
						<div class="more el-form-item" @click="moredata = !moredata">
							<span>更多</span>
							<i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
						</div>
						<div class="clearfix"></div>
						<el-collapse-transition>
							<div v-if="moredata" style="width: 100%">
								<div>
									<el-form-item label="用户" prop="uid">
										<change-user-input
											ref="changeUserInput"
											@sendValue="getUser"
											@clear="clearUid"
										></change-user-input>
									</el-form-item>
									<el-form-item label="业务员" prop="salesman_id">
										<el-select
											v-model.trim="form.salesman_id"
											placeholder="业务员"
											class="wid220"
										>
											<el-option label="所有" value="0"></el-option>
											<el-option
												v-for="(item, index) in salesman"
												:key="index"
												:label="item"
												:value="parseInt(index)"
											></el-option>
										</el-select>
									</el-form-item>
									<el-form-item label="用户分组" prop="group_id">
										<el-select
											v-model.trim="form.group_id"
											placeholder="分组"
											class="wid220"
										>
											<el-option label="所有" value="0"></el-option>
											<el-option
												v-for="(item, index) in group"
												:key="index"
												:label="item"
												:value="parseInt(index)"
											></el-option>
										</el-select>
									</el-form-item>
								</div>
							</div>
						</el-collapse-transition>
						<el-form-item>
							<el-button
								class="reset marginLeft10"
								type="primary"
								@click="reset"
								>重置</el-button
							>
							<el-button
								type="primary"
								@click="getArticleList"
								:loading="listLoading"
								>搜索</el-button
							>
						</el-form-item>
					</el-form>
				</div>
				<div class="main_table marginTop10" style="min-height: 550px">
					<el-table
						ref="multipleTable"
						v-loading="listLoading"
						:data="list"
						element-loading-text="加载中"
						fit
						border
						highlight-current-row
						:header-cell-class-name="this.$verticaline"
					>
						<el-table-column label="用户名称" prop="nickname" min-width="110">
							<template slot-scope="scope">{{ scope.row.nickname }}</template>
						</el-table-column>

						<el-table-column label="客单价" prop="avg_money" min-width="110">
							<template slot-scope="scope">
								{{ scope.row.avg_money | twoPlace }}
							</template>
						</el-table-column>

						<el-table-column
							label="下单金额"
							prop="order_money"
							min-width="110"
						>
							<template slot-scope="scope">
								{{ scope.row.order_money | twoPlace }}
							</template>
						</el-table-column>
						<el-table-column label="订单数量" prop="order_num" min-width="110">
							<template slot-scope="scope">
								{{ scope.row.order_num }}
							</template>
						</el-table-column>
						<el-table-column
							label="退货数量"
							prop="return_num"
							min-width="110"
						/>

						<el-table-column label="配送金额" prop="send_money" min-width="110">
							<template slot="header">
								<headerSortArrow
									label="send_money"
									ref="arrow1"
									@handleSort="handleSort($event, 'arrow1')"
									>配送金额</headerSortArrow
								>
							</template>
							<template slot-scope="scope">
								{{ scope.row.send_money | twoPlace }}
							</template>
						</el-table-column>
						<el-table-column
							label="退货金额"
							prop="return_money"
							min-width="110"
						>
							<template slot="header">
								<headerSortArrow
									label="return_money"
									ref="arrow2"
									@handleSort="handleSort($event, 'arrow2')"
									>退货金额</headerSortArrow
								>
							</template>
							<template slot-scope="scope">
								{{ scope.row.return_money | twoPlace }}
							</template>
						</el-table-column>
						<el-table-column
							label="管理操作"
							align="center"
							prop=""
							width="200"
							fixed="right"
						>
							<template slot-scope="scope">
								<el-link type="primary" @click="handleDetail(scope.row, 1)"
									>商品明细</el-link
								>
								<el-link type="success" @click="handleDetail(scope.row, 2)"
									>每日明细</el-link
								>
							</template>
						</el-table-column>
					</el-table>
				</div>
				<div class="main_page boxdq">
					<div></div>
					<!-- 分页 -->
					<div class="main_page_r">
						<el-pagination
							:current-page="current.page"
							:page-sizes="[20, 30, 50, 100]"
							:page-size="limit"
							layout="total, sizes, prev, pager, next, jumper"
							:total="totalCount"
							background
							@size-change="(pageSize) => changeLimit(pageSize, 1)"
							@current-change="(page) => changePage(page, 1)"
						/>
					</div>
				</div>
			</div>
		</div>

		<!-- 商品明细 -->
		<el-drawer
			class="report_drawer drawer_min900"
			:title="drawer_header_title"
			size="50%"
			:visible.sync="goods_drawer"
			direction="rtl"
			@closed="drawerClose"
		>
			<div class="drawer_container">
				<div class="drawer_container_header">
					<div class="heder_left">
						<span>{{ drawer_title }}</span>
					</div>
					<div class="header_right">
						<el-button type="primary" @click="drawer_export">导出</el-button>
					</div>
				</div>
				<div class="drawer_container_main">
					<div
						class="main_table marginTop10"
						style="min-height: 400px"
						v-if="detailType == 1"
					>
						<el-table
							ref="multipleTable"
							v-loading="drawer_loading"
							:data="drawer_list"
							element-loading-text="加载中"
							fit
							border
							highlight-current-row
							:header-cell-class-name="this.$verticaline"
						>
							<el-table-column label="商品名称" prop="goods_name">
								<template slot-scope="scope">{{
									scope.row.goods_name
								}}</template>
							</el-table-column>

							<el-table-column label="单位" prop="unit" />

							<el-table-column label="配送数量" prop="send_num">
							</el-table-column>
							<el-table-column label="配送金额" prop="order_num">
								<template slot-scope="scope">
									{{ scope.row.send_money | twoPlace }}
								</template>
							</el-table-column>
							<el-table-column label="退货数量" prop="return_num" />
							<el-table-column label="退货金额" prop="return_money">
								<template slot-scope="scope">
									{{ scope.row.return_money | twoPlace }}
								</template>
							</el-table-column>
						</el-table>
					</div>
					<div
						class="main_table marginTop10"
						style="min-height: 400px"
						v-if="detailType == 2"
					>
						<el-table
							ref="multipleTable"
							v-loading="drawer_loading"
							:data="drawer_list"
							element-loading-text="加载中"
							fit
							border
							highlight-current-row
							:header-cell-class-name="this.$verticaline"
						>
							<el-table-column label="配送日期" prop="day">
								<template slot-scope="scope">{{ scope.row.day }}</template>
							</el-table-column>
							<el-table-column label="订单数量" prop="order_num">
							</el-table-column>
							<el-table-column label="退货数量" prop="return_num" />

							<el-table-column label="配送金额" prop="send_money">
								<template slot-scope="scope">
									{{ scope.row.send_money | twoPlace }}
								</template>
							</el-table-column>
							<el-table-column label="退货金额" prop="return_money">
							</el-table-column>
						</el-table>
					</div>
					<div class="main_page boxdq">
						<div></div>
						<!-- 分页 -->
						<div class="main_page_r">
							<el-pagination
								:current-page="drawer_current.page"
								:page-sizes="[20, 30, 50, 100]"
								:page-size.sync="drawer_limit"
								layout="total, sizes, prev, pager, next, jumper"
								:total="drawer_totalCount"
								background
								@size-change="(pageSize) => changeLimit(pageSize, 2)"
								@current-change="(page) => changePage(page, 2)"
							/>
						</div>
					</div>
				</div>
			</div>
		</el-drawer>
	</div>
</template>

<script>
	import {
		get_saleMember,
		get_saleMemberGoods,
		get_saleMemberDetail,
		export_exportSaleMember,
		export_exportSaleMemberGoods,
		export_exportSaleMemberDetail,
		get_report_base,
	} from "@/api/report/index";
	import { apiUrl, objKeySort, Md5 } from "@/utils/public";
	import { report_shortcuts } from "@/options/new_dateOption";
	import { parseTime, getNowMonth, exportMethod } from "@/utils/index";
	import headerSortArrow from "@/views/report/components/headerSortArrow";

	export default {
		components: {
			headerSortArrow,
		},
		data() {
			return {
				pickerOptions: report_shortcuts,
				// 时间
				parseTime,
				getNowMonth,
				exportMethod,
				// 分页
				current: {
					page: 1,
					per_page: 20,
				},
				limit: 20,
				totalCount: 1,
				// 列表数据
				list: [],
				group: {},
				salesman: {},
				listLoading: false,
				// 搜索
				form: {
					start_time: "",
					end_time: "",
					uid: "",
					salesman_id: "",
					group_id: "",
					is_piroment: "",
					date_type: "1",
					order_by: "",
					order_type: "", //1升序  2降序
				},
				is_piroment: {},
				establishTime: [],
				moredata: false,
				goods_drawer: false, //商品明细抽屉
				detailType: 1, //1 商品明细   2每日明细
				drawer_title: "用户名称",
				drawer_header_title: "商品明细",
				drawer_list: [],
				drawer_loading: false,
				// 分页
				drawer_current: {
					page: 1,
					per_page: 20,
				},
				drawer_limit: 20,
				drawer_totalCount: 1,
				drawer_form: {
					start_time: "",
					end_time: "",
					uid: "",
					date_type: "",
				},
			};
		},
		created() {
			this.establishTime = this.getNowMonth();
			this.get_report_base();
			this.get_saleMember();
		},
		watch: {
			"form.date_type": {
				handler(val) {
					this.drawer_form.date_type = val;
				},
				deep: true,
				immediate: true,
			},
		},
		methods: {
			// 基本信息
			get_report_base() {
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name
				);
				get_report_base(data).then((res) => {
					if (res.errcode == 0) {
						this.is_piroment = res.data.is_piroment;
					}
				});
			},
			// 获取用户
			getUser(data) {
				let uid = data.info.user_id;
				this.form.uid = uid;
			},
			// 清除用户的uid
			clearUid() {
				this.form.uid = "";
			},
			get_saleMember() {
				this.listLoading = true;
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					this.list_form(),
					this.current
				);
				get_saleMember(data).then((res) => {
					if (res.errcode == 0) {
						this.list = res.data.list;
						this.group = res.data.group;
						this.salesman = res.data.salesman;
						this.totalCount = res.data.total;
					} else {
						this.$message.error(res.errmsg);
					}
					this.listLoading = false;
				});
			},
			// 重置
			reset() {
				this.$refs["userListFrom"].resetFields();
				if (this.moredata) {
					this.$refs["changeUserInput"].name = "";
				}
				this.establishTime = this.getNowMonth();
				this.getArticleList();
			},
			// 搜索
			getArticleList() {
				this.current.page = 1;
				this.get_saleMember();
			},
			// 分页
			changeLimit(pageSize, type) {
				switch (type) {
					case 1:
						this.current.per_page = pageSize;
						this.get_saleMember();
						break;
					case 2:
						this.drawer_current.per_page = pageSize;
						if (this.detailType == 1) {
							this.get_saleMemberGoods(this.drawer_form);
						} else if (this.detailType == 2) {
							this.get_saleMemberDetail(this.drawer_form);
						}
						break;
				}
			},
			changePage(page, type) {
				switch (type) {
					case 1:
						this.current.page = page;
						this.get_saleMember();
						break;
					case 2:
						this.drawer_current.page = page;
						if (this.detailType == 1) {
							this.get_saleMemberGoods(this.drawer_form);
						} else if (this.detailType == 2) {
							this.get_saleMemberDetail(this.drawer_form);
						}
						break;
				}
			},

			list_form() {
				let form = JSON.parse(JSON.stringify(this.form));
				if (this.establishTime) {
					form.start_time = this.establishTime[0] ? this.establishTime[0] : "";
					form.end_time = this.establishTime[1] ? this.establishTime[1] : "";
				}
				return form;
			},
			// 排序
			handleSort(data, ref) {
				let arr = ["arrow1", "arrow2"].filter((e) => e != ref);
				arr.map((e) => {
					this.$refs[e].selected = false;
				});
				this.form.order_by = data.label;
				this.form.order_type = data.type;
				this.get_saleMember();
			},
			// 用户导出
			exportform() {
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					this.list_form()
				);
				export_exportSaleMember(data).then((res) => {
					this.exportMethod(res, "销售报表-用户");
				});
			},
			// 侧滑导出
			drawer_export() {
				switch (this.detailType) {
					case 1:
						this.export_exportSaleMemberGoods();
						break;
					case 2:
						this.export_exportSaleMemberDetail();
						break;
				}
			},
			//商品明细导出
			export_exportSaleMemberGoods() {
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					this.drawer_form
				);
				export_exportSaleMemberGoods(data).then((res) => {
					this.exportMethod(res, "销售报表-用户-商品明细");
				});
			},
			// 每日明细导出
			export_exportSaleMemberDetail() {
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					this.drawer_form
				);
				export_exportSaleMemberDetail(data).then((res) => {
					this.exportMethod(res, "销售报表-用户-每日明细");
				});
			},
			// 日期选择
			dateChange(val) {
				let start = new Date(val[0]).getTime();
				let end = new Date(val[1]).getTime();
				let diff = start - end;
				if (Math.abs(diff) > 3600 * 1000 * 24 * 60) {
					this.$message.warning("选择时间跨度不能超过两个月");
					this.establishTime = this.getNowMonth();
				}
				this.get_saleMember();
			},
			// 商品明细
			get_saleMemberGoods(form) {
				this.drawer_loading = true;
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					form,
					this.drawer_current
				);
				get_saleMemberGoods(data).then((res) => {
					if (res.errcode == 0) {
						this.drawer_list = res.data.list;
						this.drawer_totalCount = res.data.total;
						this.drawer_loading = false;
					} else {
						this.$message.warning(res.errmsg);
					}
				});
			},
			// 每日明细
			get_saleMemberDetail(form) {
				this.drawer_loading = true;
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					form,
					this.drawer_current
				);
				get_saleMemberDetail(data).then((res) => {
					if (res.errcode == 0) {
						this.drawer_list = res.data.list;
						this.drawer_totalCount = res.data.total;
						this.drawer_loading = false;
					} else {
						this.$message.warning(res.errmsg);
					}
				});
			},
			handleDetail(row, type) {
				this.detailType = type;
				this.drawer_title = row.nickname;
				let form = {
					start_time: "",
					end_time: "",
					uid: row.uid,
					is_piroment: this.form.is_piroment,
					date_type: this.form.date_type,
				};
				if (this.establishTime) {
					form.start_time = this.establishTime[0] ? this.establishTime[0] : "";
					form.end_time = this.establishTime[1] ? this.establishTime[1] : "";
				}
				this.drawer_form = form;
				switch (type) {
					case 1:
						this.drawer_header_title = "商品明细";
						this.get_saleMemberGoods(form);
						break;
					case 2:
						this.drawer_header_title = "每日明细";
						this.get_saleMemberDetail(form);
						break;
				}
				this.goods_drawer = true;
			},
			// 侧滑删除事件
			drawerClose() {
				this.drawer_list = [];
				this.drawer_current.page = 1;
				this.drawer_current.per_page = 20;
				this.drawer_limit = 20;
				this.drawer_form = {
					start_time: "",
					end_time: "",
					uid: "",
					is_piroment: "",
				};
			},
		},
	};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
	.mainor_form form {
		width: calc(100% - 180px);
	}
}

// 支付状态
.success {
	color: $green;
}
.unpaid {
	color: $colour_red;
}
// 订单状态
.order0 {
	color: $order_color1;
}
.order1 {
	color: $order_color2;
}
.order2 {
	color: $order_color3;
}
.order4 {
	color: $order_color4;
}
</style>
